package com.eternal.log.adapter;

import androidx.paging.PagedList;
import androidx.paging.PagedListAdapter;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;

import android.util.SparseArray;
import android.view.ViewGroup;

import com.eternal.base.concat.LogExtra;
import com.eternal.base.database.entity.Log;
import com.eternal.log.R;

import java.util.Calendar;

/**
 * Created by Administrator
 * On 2020/5/14
 * Description:
 * @author Administrator
 */
public class LogAdapter extends PagedListAdapter<Log, LogHolder> {
    private static final int CONTENT=0;
    private static final int HEADER=1;
    private static final int FOOBAR =2;
    private static final int FOOBAR_HEADER =3;

    private long start;
    private boolean isDegree;
    private SparseArray<String> notifyCache;
    private Calendar first,last;
    private long end;

    public LogAdapter(LogExtra extra) {
        super(diff);
        first=Calendar.getInstance();
        last=Calendar.getInstance();
        setExtra(extra);
    }

    public void setExtra(LogExtra extra){
        if(isDegree!=extra.isDegree){
            isDegree=extra.isDegree;
            notifyItemRangeChanged(0, getItemCount());
        }
        notifyCache=extra.notifyName;
        if(extra.time==0){
            start=System.currentTimeMillis();
        }else{
            start=extra.time;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            end=getItem(position).time;
            return HEADER;
        }else if(position==getItemCount()-1){
            return FOOBAR;
        }
        first.setTimeInMillis(getItem(position).time);
        last.setTimeInMillis(getItem(position-1).time);
        if(first.get(Calendar.DAY_OF_MONTH)!=last.get(Calendar.DAY_OF_MONTH)||
                first.get(Calendar.MONTH)!=last.get(Calendar.MONTH)||
                first.get(Calendar.YEAR)!=last.get(Calendar.YEAR)){
            return FOOBAR_HEADER;
        }else{
            return CONTENT;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @NonNull
    @Override
    public LogHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        switch (i){
            case HEADER:
                return new LogHeaderHolder(viewGroup);
            case FOOBAR:
                return new LogHolder(viewGroup, R.layout.log_item_foobar);
            case FOOBAR_HEADER:
                return new LogHeaderHolder(viewGroup, R.layout.log_item_foobar_header);
            default:
                return new LogHolder(viewGroup);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull LogHolder viewHolder, int i) {
        viewHolder.init(getItem(i), start,notifyCache, isDegree);
    }

    public boolean isClear(){
        return start>=end;
    }

    public void clear(){
        this.start=System.currentTimeMillis();
        notifyItemRangeChanged(0, getItemCount());
    }

    private static DiffUtil.ItemCallback<Log> diff=new DiffUtil.ItemCallback<Log>() {
        @Override
        public boolean areItemsTheSame(@NonNull Log log, @NonNull Log t1) {
            return log.id==t1.id&&log.time==t1.time;
        }

        @Override
        public boolean areContentsTheSame(@NonNull Log log, @NonNull Log t1) {
            return log.id==t1.id&&log.time==t1.time;
        }
    };
}
